<!-- eslint-disable vue/no-useless-template-attributes -->
<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="交易日期" name="tradeDate">
						<a-range-picker v-model:value="searchFormState.tradeDate" value-format="YYYY-MM-DD" show-time />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="合约代码" name="contractCode">
						<a-input v-model:value="searchFormState.contractCode" placeholder="请输入合约代码" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="合约名称" name="contractName">
						<a-input v-model:value="searchFormState.contractName" placeholder="请输入合约名称" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="设置止损时间" name="lossDate">
						<a-range-picker v-model:value="searchFormState.lossDate" show-time />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="数据来源" name="datesource">
						<a-input v-model:value="searchFormState.datesource" placeholder="请输入数据来源" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="批次号" name="batchId">
						<a-input v-model:value="searchFormState.batchId" placeholder="请输入批次号" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="tableRef.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
					<a @click="toggleAdvanced" style="margin-left: 8px">
						{{ advanced ? '收起' : '展开' }}
						<component :is="advanced ? 'up-outlined' : 'down-outlined'" />
					</a>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="tableRef"
			:scroll="{ x: 2000, y: 500 }"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('qhHistoryAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>

					<a-button @click="ImpExpRef.onOpen()">
						<template #icon><import-outlined /></template>
						<span>{{ $t('common.imports') }}</span>
					</a-button>
					<a-button @click="exportBatchQhHistoryVerify">
						<template #icon><export-outlined /></template>
						{{ $t('user.batchExportButton') }}
					</a-button>
					<xn-batch-delete
						v-if="hasPerm('qhHistoryBatchDelete')"
						:selectedRowKeys="selectedRowKeys"
						@batchDelete="deleteBatchQhHistory"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'openOrder'">
					{{ $TOOL.dictTypeData('Y_N', record.openOrder) }}
				</template>
				<template v-if="column.dataIndex === 'closeOrder'">
					{{ $TOOL.dictTypeData('Y_N', record.closeOrder) }}
				</template>
				<template v-if="column.dataIndex === 'upDown'">
					{{ $TOOL.dictTypeData('Y_N', record.upDown) }}
				</template>
				<template v-if="column.dataIndex === 'lossOrder'">
					{{ $TOOL.dictTypeData('Y_N', record.lossOrder) }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('qhHistoryEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['qhHistoryEdit', 'qhHistoryDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteQhHistory(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('qhHistoryDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />
	<ImpExp ref="ImpExpRef" />
</template>

<script setup name="qhhistory">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import Form from './form.vue'
	import qhHistoryApi from '@/api/biz/qhHistoryApi'
	import downloadUtil from '@/utils/downloadUtil'
	import ImpExp from './impExp.vue'
	import { message, Empty } from 'ant-design-vue'
	const searchFormState = ref({})
	const searchFormRef = ref()
	const tableRef = ref()
	const formRef = ref()
	const ImpExpRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	// 查询区域显示更多控制
	const advanced = ref(false)
	const toggleAdvanced = () => {
		advanced.value = !advanced.value
	}
	const columns = [
		{
			title: '交易日期',
			dataIndex: 'tradeDate',
			width: 100,
			fixed: 'left'
		},
		{
			title: '合约代码',
			dataIndex: 'contractCode',
			fixed: 'left'
		},
		{
			title: '合约名称',
			dataIndex: 'contractName',
			fixed: 'left'
		},
		{
			title: '数据来源',
			dataIndex: 'datesource',
			fixed: 'left'
		},
		{
			title: '批次号',
			dataIndex: 'batchId',
			fixed: 'left'
		},
		{
			title: '开盘价',
			dataIndex: 'openPrice'
		},
		{
			title: '收盘价',
			dataIndex: 'closePrice'
		},
		{
			title: '昨结',
			dataIndex: 'yesterdayConclusion'
		},
		{
			title: '今结',
			dataIndex: 'todayConclusion'
		},
		{
			title: '涨跌1',
			dataIndex: 'upDownOne'
		},
		{
			title: '成交量',
			dataIndex: 'finishNumber'
		},
		{
			title: '成交金额',
			dataIndex: 'finishAmount'
		},
		{
			title: '持仓量',
			dataIndex: 'positionNumber'
		},
		{
			title: '增减量',
			dataIndex: 'changeNumber'
		},
		{
			title: '最高价',
			dataIndex: 'highestPrice'
		},
		{
			title: '最低价',
			dataIndex: 'lowestPrice'
		},
		{
			title: '设置止损时间',
			dataIndex: 'lossDate'
		},
		{
			title: '是否开盘单',
			dataIndex: 'openOrder'
		},
		{
			title: '是否收盘单',
			dataIndex: 'closeOrder'
		},
		{
			title: '最后收益',
			dataIndex: 'finalIncome'
		},
		{
			title: '止损价',
			dataIndex: 'lossPrice'
		},
		{
			title: '涨或跌',
			dataIndex: 'upDown'
		},
		{
			title: '是否止损单',
			dataIndex: 'lossOrder'
		}
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['qhHistoryEdit', 'qhHistoryDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			fixed: 'right',
			width: 150
		})
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		const searchFormParam = cloneDeep(searchFormState.value)
		// tradeDate范围查询条件重载
		if (searchFormParam.tradeDate) {
			searchFormParam.startTradeDate = searchFormParam.tradeDate[0]
			searchFormParam.endTradeDate = searchFormParam.tradeDate[1]
			delete searchFormParam.tradeDate
		}
		// lossDate范围查询条件重载
		if (searchFormParam.lossDate) {
			searchFormParam.startLossDate = searchFormParam.lossDate[0]
			searchFormParam.endLossDate = searchFormParam.lossDate[1]
			delete searchFormParam.lossDate
		}
		return qhHistoryApi.qhHistoryPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteQhHistory = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		qhHistoryApi.qhHistoryDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchQhHistory = (params) => {
		qhHistoryApi.qhHistoryDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
	// 批量导出校验并加参数
	const exportBatchQhHistoryVerify = (parameter) => {
		const searchFormParam = cloneDeep(searchFormState.value)
		// tradeDate范围查询条件重载
		if (searchFormParam.tradeDate) {
			searchFormParam.startTradeDate = searchFormParam.tradeDate[0]
			searchFormParam.endTradeDate = searchFormParam.tradeDate[1]
			delete searchFormParam.tradeDate
		} else {
			message.warning('请输入交易时间')
			return
		}
		// lossDate范围查询条件重载
		if (searchFormParam.lossDate) {
			searchFormParam.startLossDate = searchFormParam.lossDate[0]
			searchFormParam.endLossDate = searchFormParam.lossDate[1]
			delete searchFormParam.lossDate
		}
		exportBatchQhHistory(Object.assign(parameter, searchFormParam))
	}
	// 批量导出
	const exportBatchQhHistory = (params) => {
		qhHistoryApi.qhHistoryExport(params).then((res) => {
			downloadUtil.resultDownload(res)
			tableRef.value.clearSelected()
		})
	}
</script>
@/api/biz/qhHistoryApi